<?php

namespace Kateba\SiteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Kateba\SiteBundle\Entity\Comment;

class BlogController extends Controller {

    /**
     * this function get all comments for a post
     * @author ahmed
     * @param integer $id
     */
    public function getAllPostCommentsAction($id) {
        //get the entity manager
        $em = $this->getDoctrine()->getEntityManager();
        $postRepo = $em->getRepository('KatebaSiteBundle:Post');
        $postCommentRepo = $em->getRepository('KatebaSiteBundle:Comment');

        //get the article object
        $post = $postRepo->find($id);
        if (!$post) {
            return new Response('failed');
        }

        $postComments = $post->getComments();
        return $this->render('KatebaSiteBundle:Blog:getAllPostComments.html.twig', array(
                    'postComments' => $postComments
                ));
    }

    /**
     * this function used to add comment to post by loggedin user
     * @author ahmed
     * @param integer $id
     * @param string $comment
     */
    public function postCommentAction($id, $comment, $_locale) {
        //get the session
        $session = $this->get('session');
        if (FALSE === $this->get('security.context')->isGranted('ROLE_USER')) {
            //set the url in session
            $session->set('postComment', $comment);
            return new Response('login');
        }

        //get the entity manager
        $em = $this->getDoctrine()->getEntityManager();
        $postRepo = $em->getRepository('KatebaSiteBundle:Post');
        $postCommentRepo = $em->getRepository('KatebaSiteBundle:Comment');

        //get the article object
        $post = $postRepo->find($id);
        if (!$post) {
            return new Response('failed');
        }

        //get logedin user objects
        $user = $this->get('security.context')->getToken()->getUser();

        //create new article comment
        $newComment = new Comment();
        $newComment->setPost($post);
        $newComment->setUser($user);
        $newComment->setComment($comment);
        $post->setNoComments($post->getNoComments() + 1);
        $em->persist($newComment);
        $em->flush();

        //remove comment session
        $session->remove('postComment');
        return $this->render('KatebaSiteBundle:Blog:postComment.html.twig', array(
                    'newComment' => $newComment
                ));
    }

    /**
     * this function used to show blog
     * @author ahmed
     * @param integer $id
     */
    public function showBlogAction($_locale, $id) {
        $em = $this->getDoctrine()->getEntityManager();
        $postRepo = $em->getRepository('KatebaSiteBundle:Post');
        $postCommentRepo = $em->getRepository('KatebaSiteBundle:Comment');

        $post = $postRepo->find($id);
        if (!$post) {
            throw $this->createNotFoundException('Unable to find this post.');
        }

        //get the session
        $session = $this->get('session');
        //set the url in session
        $session->set('redirectUrl', $this->generateUrl('show_blog', array('id' => $id, '_locale' => $_locale)));

        $post->setNoViews($post->getNoViews() + 1);
        $em->flush();

        //get post comment
        $comments = array_reverse($postCommentRepo->getPostComments($post->getId(), $this->container->getParameter('post_page_comments_number')));
        $commentsCount = $postCommentRepo->countPostComments($post->getId());

        return $this->render('KatebaSiteBundle:Blog:showBlog.html.twig', array(
                    'locale' => $_locale,
                    'post' => $post,
                    'comments' => $comments,
                    'post_page_comments_number' => $this->container->getParameter('post_page_comments_number'),
                    'commentsCount' => $commentsCount['0']['commentsCount']
                ));
    }

    /**
     * this function used to list all site blogs
     * @author ahmed
     */
    public function listBlogsAction($_locale, Request $request) {
        $em = $this->getDoctrine()->getEntityManager();
        $postRepo = $em->getRepository('KatebaSiteBundle:Post');

        $itemsPerPage = $this->container->getParameter('number_of_blogs_per_page');
        $page = $request->get('page', 1);
        $blogTitle = $request->get('title');

        $data = $postRepo->getAllBlogs($page, $itemsPerPage, $blogTitle);
        $entities = $data['entities'];
        $count = $data['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }

        return $this->render('KatebaSiteBundle:Blog:listBlogs.html.twig', array(
                    'locale' => $_locale,
                    'posts' => $entities,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber,
                    'blogTitle' => $blogTitle
                ));
    }

}
